<?php

/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application\Controller;

use Com\Mvc\Controller\BaseController;
use Zend\View\Model\ViewModel;

class IndexController extends BaseController {
	
	public function __construct(){
		parent::__construct();
		$this->demoModel = $this->serviceManager->get('Test\demo');
	}
	public function indexAction() {
		$view = new ViewModel();
		echo 123;
		return $view;
	}
	//#########################分页action######################//
	/**
	 * 分页首次进入项
	 * Enter description here ...
	 */
	public function pageMainAction(){
		$view = new ViewModel();
		$pageView = $this->getPage();
		$view->addChild($pageView,'page');
		return $view;
	}
	/**
	 * 获得分页数据
	 * Enter description here ...
	 * @param int $num
	 */
	private function getPage($num=1){
		$view = new ViewModel();
		$data = $this->demoModel->getPaginatorList(null,array('id','name','age'),array('id'=>'desc'),1,$num);
		$view->setVariables($data);
		$view->setTemplate('application/index/page');
		return $view;
	}
	/**
	 * 异步分页刷星控制器
	 * Enter description here ...
	 */
	public function changePageAction(){
		$postNum= $this->getRequest()->getPost('currentPageNumber');
		$view = $this->getPage($postNum);
		$view->setTerminal(true);
		return $view;
	}
	//#########################分页action结束######################//

	public function testDb(){
		$server = $this->getServiceLocator();
		//       	$config = $server->get('config');
		//       	echo "<pre>";
		//       	print_r($config);

		//		trigger_error("yuhaya",E_USER_NOTICE);
		$data = $this->demoModle->getRowList(array('name','id','age'));

		echo "<pre>";
		print_r($data);
	}

	public function testEvent(){
		$view = new ViewModel();
		$event = $this->getMyEvent();
		$view->setTerminal(true);
		return $view;
	}

	protected function getMyEvent() {
		$outVar = 100;
		$myEvent = new \Com\Test\event\ownEvent();
		$myEvent->setEventManager(new \Zend\EventManager\EventManager());
		$myEvent->getEventManager()->attach('log', function($e)use($outVar) {
			$eventName = $e->getName();
			$target = get_class($e->getTarget());
			$par = $e->getParams();
			$data = array(
                'name' => $eventName,
                'target' => $target,
                'params' => $par
            );
            echo "<pre>";
            print_r($data);
            print_r($outVar);
           
        });
        print_r($myEvent->getEventManager()->getIdentifiers());
        $myEvent->log(array("one","two"));
        
    }

}
